package com.ruoyi.project.kbjz.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.kbjz.domain.KbjzRegister;
import com.ruoyi.project.kbjz.domain.vo.KbjzVoCondition;
import com.ruoyi.project.kbjz.service.IKbjzRegisterService;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/kbjz/register")
public class KbjzRegisterController extends BaseController {
    @Autowired
    private IKbjzRegisterService kbjzRegisterService;
    @Autowired
    private ISysUserService sysUserService;
    @GetMapping("/selectRegisterByCondition")
    public TableDataInfo selectRegisterByCondition( KbjzVoCondition kbjzVoCondition){
        //判断是否需要进行部门检查
        if (kbjzVoCondition.getFlag()){
            String username = SecurityUtils.getUsername();
            SysUser sysUser = sysUserService.selectUserByUserName(username);
            kbjzVoCondition.setDeptId(sysUser.getDeptId());
        }
        startPage();
        List<KbjzRegister> kbjzRegisters = kbjzRegisterService.selectRegisterByVoCondition(kbjzVoCondition);
        return getDataTable(kbjzRegisters);
    }

    @PutMapping("/handleCharge")
    public AjaxResult handleCharge(Integer registerStatus,String registerId,Integer cost){
        //先调用支付宝沙箱进行支付

        //根据支付结果，修改挂号状态
        boolean updateResult = kbjzRegisterService.updateRegisterStatus(registerStatus, registerId);
        //最终返回结果
        if (updateResult){
            return AjaxResult.success("收费成功");
        }else{
            return AjaxResult.error("收费失败");
        }
    }

    /**
     * 修改挂单号的状态
     * @param registerStatus
     * @param registerId
     * @return
     */
    @PutMapping("/updateRegisterStatus")
    public AjaxResult updateRegisterStatus(Integer registerStatus,String registerId){
        boolean updateResult = kbjzRegisterService.updateRegisterStatus(registerStatus, registerId);
        //最终返回结果
        if (updateResult){
            return AjaxResult.success();
        }else{
            return AjaxResult.error("网络延时，请重新保存");
        }
    }

}
